//
// ********************************************************************
// * License and Disclaimer                                           *
// *                                                                  *
// * The  Geant4 software  is  copyright of the Copyright Holders  of *
// * the Geant4 Collaboration.  It is provided  under  the terms  and *
// * conditions of the Geant4 Software License,  included in the file *
// * LICENSE and available at  http://cern.ch/geant4/license .  These *
// * include a list of copyright holders.                             *
// *                                                                  *
// * Neither the authors of this software system, nor their employing *
// * institutes,nor the agencies providing financial support for this *
// * work  make  any representation or  warranty, express or implied, *
// * regarding  this  software system or assume any liability for its *
// * use.  Please see the license in the file  LICENSE  and URL above *
// * for the full disclaimer and the limitation of liability.         *
// *                                                                  *
// * This  code  implementation is the result of  the  scientific and *
// * technical work of the GEANT4 collaboration.                      *
// * By using,  copying,  modifying or  distributing the software (or *
// * any work based  on the software)  you  agree  to acknowledge its *
// * use  in  resulting  scientific  publications,  and indicate your *
// * acceptance of all terms of the Geant4 Software license.          *
// ********************************************************************
//
//
/// \file B1/src/SteppingAction.cc
/// \brief Implementation of the B1::SteppingAction class

#include "SteppingAction.hh"

#include <atomic>

#include "DetectorConstruction.hh"
#include "EventAction.hh"
#include "G4Event.hh"
#include "G4LogicalVolume.hh"
#include "G4RunManager.hh"
#include "G4Step.hh"       // step info
#include "G4StepPoint.hh"  //包含Geant4中处理每个相互作用点的头文件
#include "G4Track.hh"      //包含Geant4中Track的头文件
#include "MyEventID.h"

namespace B1
{

    //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

    SteppingAction::SteppingAction(EventAction* eventAction) : _fEventAction(eventAction) {}

    //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

    ///xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 形参为当前Step的指针
    void SteppingAction::UserSteppingAction(const G4Step* step)
    {
        const int evtid = _fEventAction->GetEvtID();
        auto* const track = step->GetTrack(); //获取当前Step所在的Track的指针
        auto trackId = track->GetTrackID();           //获取当前Step所在的Track的id
        auto parentId = track->GetParentID();         //获取当前Step所在的Track的父track的id，如果其本身为初级track，则为0
        auto stepId = track->GetCurrentStepNumber();  //获取当前Step的id

        auto particleId = track->GetParticleDefinition()->GetParticleName();  //获取当前Step的粒子的名称
        auto edepStep = (step->GetTotalEnergyDeposit());                      //获取在本Step中沉积的能量
        auto energy = (step->GetPreStepPoint()->GetKineticEnergy());          //获取当前Step的粒子的动能
        G4cout << evtid << " " << parentId << " " << trackId << " " << stepId << " " << particleId << " " << edepStep << " " << energy << " "
               << G4endl;
        //将上述信息输出至显示屏中

        ///===================================================================
        if (!_fScoringVolume)
        {
            const auto detConstruction = static_cast<const DetectorConstruction*>(G4RunManager::GetRunManager()->GetUserDetectorConstruction());
            _fScoringVolume = detConstruction->GetScoringVolume();
        }

        /// get volume of the current step
        G4LogicalVolume* volume = step->GetPreStepPoint()->GetTouchableHandle()->GetVolume()->GetLogicalVolume();

        /// check if we are in scoring volume
        if (volume != _fScoringVolume) return;

        /// collect energy deposited in this step
        _fEventAction->AddEdep(edepStep);
    }

    //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

}  // namespace B1
